package com.xb.p32_sqlitedemo1;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

class MyDBHelper extends SQLiteOpenHelper {

    public MyDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据库的时候执行
        String sql="CREATE TABLE user(" +
                "userid integer primary key autoincrement," +
                "username text not null," +
                "password text not null)";
        db.execSQL(sql);
    }

    //在数据库表中增加一列 age integer
    String sql_version_2="alter table user add column age integer";
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //添加表，或者表结构变更的时候执行
        if(oldVersion<2){//当下一次版本号变成2的时候将不再执行
            db.execSQL(sql_version_2);
        }
    }
}
